//全局公共变量
export default {
  server : {
    online : ['www.dongfeng-nissan.com.cn','www.uat.dongfeng-nissan.com.cn']
  },
  rex: {
    username: /^[A-Za-z0-9\u4e00-\u9fa5]+$/,
    //手机号
    mobile: /^1[3|4|5|6|7|8|9]\d{9}$/
  },
  info: {
    remW : 750
  },
  // swiper动画拓展
  _swiper : {
    startAnimate (ele){
      const allEle = $(ele.$el).find('.animated')
      const activeEle = $(ele.$el).find('.swiper-slide-active .animated')
      // 隐藏所有动画元素
      allEle.hide()
      // 显示当前动画元素
      activeEle.each((index, item) => {
        const delay = $(item).attr('swiper-delay')
        $(item).css({'animation-delay': delay, '-webkit-animation-delay': delay}).show()
      })
    }
  },
  // cookie操作
  cookie : {
    get (c_name) {
      if (document.cookie.length > 0) {
        let c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
          c_start = c_start + c_name.length + 1
          let c_end = document.cookie.indexOf(";", c_start);
          if (c_end == -1) c_end = document.cookie.length
          return unescape(document.cookie.substring(c_start, c_end))
        }
      }
      return ""
    },
    set (c_name, value, expiredays) {
      let exdate = new Date();
      exdate.setDate(exdate.getDate() + expiredays);
      document.cookie = c_name + "=" + value + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ';path=/';
    }
  },
  // query 操作
  query : {
    get: function (name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      const r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
  },

  // 滚动页面到指定位置
  toScrollElement(ele) {
    if (ele) {
      setTimeout(() => {
        const offTop = document.querySelector(ele).offsetTop;
        document.documentElement.scrollTop = document.body.scrollTop = offTop;
      }, 100)
    }
  },

  Is : {
    Pc() {
      let userAgentInfo = navigator.userAgent;
      let Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"];
      let flag = true;
      for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
          flag = false;
          break;
        }
      }
      return flag;
    },
    Wx() {
      let ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
      } else {
        return false;
      }
    },
    Android: function(){
      let u = navigator.userAgent;
      let ua = navigator.userAgent.toLowerCase();
      // 判断安卓
      return (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1)
    }
  },

  // 加载js
  loadScript(src, callback) {
    let script = document.createElement('script'),
      head = document.getElementsByTagName('head')[0];
    script.type = 'text/javascript';
    script.charset = 'UTF-8';
    script.src = src;
    if (script.addEventListener) {
      script.addEventListener('load', function () {
        callback();
      }, false);
    } else if (script.attachEvent) {
      script.attachEvent('onreadystatechange', function () {
        let target = window.event.srcElement;
        if (target.readyState == 'loaded') {
          callback();
        }
      });
    }
    head.appendChild(script);
  },

  // 微信自定义函数
  _Wx () {
    const that = this
    return {
      wxApi: '',
      wxConfig: function (config) {
        wx.config({
          debug: false,
          appId: config.appId,
          timestamp: config.timestamp,
          nonceStr: config.nonceStr,
          signature: config.signature,
          jsApiList: [
            'chooseImage', 'uploadImage', 'previewImage', 'onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareWeibo'
          ]
        });

      },
      wxShare: function (Option) {
        if (!that.Is.Wx()) return;
        let _this = this,
          initWxShare = (wxConfig) => {
            _this.wxConfig(wxConfig);
            //初始化
            if (!Option) Option = {};
            let title = Option.title ? Option.title : "第九届品质论坛即将盛大开幕",
              summary = Option.cont ? Option.cont : "东风日产&东风启辰第9届品质论坛邀请函",
              link = Option.url ? Option.url : location.href,
              thumb = Option.logo ? Option.logo : 'http://' + _this.on_server + __uri('/web/img/icon-share.jpg'),
              content = {
                title: title,
                desc: summary,
                link: link,
                imgUrl: thumb,
                /*type: '',
                dataUrl: '',*/
                success: function () {
                  if (Option.success) {
                    Option.success();
                  }
                },
                cancel: function () {
                }
              }
            wx.ready(function () {
              wx.onMenuShareTimeline(content);
              wx.onMenuShareAppMessage(content);
              wx.onMenuShareQQ(content);
              wx.onMenuShareWeibo(content);
              // alert(JSON.stringify(content))
            });
          }

        //如果有拿到过key，就直接执行微信sdk
        if (_this.wxApi) {
          initWxShare(_this.wxApi)
        } else {
          that.loadScript('//res.wx.qq.com/open/js/jweixin-1.2.0.js', function () {
            let hostUrl = '';
            that.server.online.forEach((item) => {
              if(item == location.hostname){
                hostUrl = '//weixin.dongfeng-nissan.com.cn'
              }
            })
            $.ajax({
              url: hostUrl + '/lyapi.php?a=CommonApi&m=jssdk&key=0AE6BFEBDEBFC9D955F450954AEAD8E3&t=' + new Date().getTime(),
              data: {
                url: location.href
              },
              dataType: 'jsonp',
              success: function (json) {
                _this.wxApi = json;
                initWxShare(_this.wxApi);
              }
            })
          })
        }
      },
      wxAccredit: function () {
        location.replace("https://weixin.dongfeng-nissan.com.cn/lyapi.php?a=CommonApi&m=userOauth&key=0AE6BFEBDEBFC9D955F450954AEAD8E3&url="+encodeURI(location.href));
      }
    }
  }
}
